package com.cie2.janji.services;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.cie2.janji.entities.BaseEntity;

public class BaseEntityService {

	private Session session;
	
	public BaseEntityService(Session session) {
		this.session = session;
	}
	
	public void add(BaseEntity baseEntity) {
		session.persist(baseEntity);
		session.flush();
	}
	
	public List getAll() {
		return session.createCriteria(BaseEntity.class).list();
	}

	public BaseEntity load(Long entityId) {
		return (BaseEntity) session.load(BaseEntity.class, entityId);
	}
	
	public BaseEntity getByName(String name) {
		return (BaseEntity) session.createCriteria(BaseEntity.class)
				.add(Restrictions.eq("name", name))
				.uniqueResult();
	}
}
